package yxy.game.pm2.module.player;

import yxy.apple.util.ActiveUtil;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.function;
import yxy.cherry.data.function.Function;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.db.dao.MySQLServerDAO;
import yxy.game.pm2.bean.db.module.server;
import yxy.game.pm2.module.conquest.ConquestModule;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;

public class FunctionHandle {

    //判断开服天数，玩家等级和征战关数来开启功能
    public boolean check(Function func, String playerCode) {
        try {
            function data = DataCenter.getData(func.getId(), function.class);
            if (data == null) {
                return false;
            }

            //获取用户等级
            PlayerBean playerBean = PlayerBean.get(playerCode);
            if (playerBean.getLevel() < data.getPlayerlv()) {
                return false;
            }

            //获取开服天数
            Long days = getServerStartTime(playerBean);
            if (days < data.getInitday()) {
                return false;
            }

            //获取征战关数
            ConquestModule module = ConquestModule.get(playerCode);
            return module.getMapId() >= data.getMapnum();
        } catch (Exception e) {
        }
        return false;
    }

    //判断开服天数，玩家等级和征战关数来开启功能
    public boolean check(Function func, PlayerBean playerBean) {
        if (playerBean == null) {
            return false;
        }
        try {
            function data = DataCenter.getData(func.getId(), function.class);
            String playerCode = playerBean.getCode();
            if (data == null) {
                return false;
            }
            if (playerBean.getLevel() < data.getPlayerlv()) {
                return false;
            }

            //获取开服天数
            Long days = getServerStartTime(playerBean);
            if (days < data.getInitday()) {
                return false;
            }

            //获取征战关数
            ConquestModule module = ConquestModule.get(playerCode);
            return module.getMapId() >= data.getMapnum();

        } catch (Exception e) {
        }
        return false;
    }

    //根据世界等级开启
    public boolean check(Function func, AreaBean areaBean) {
        try {
            function data = DataCenter.getData(func.getId(), function.class);
            if (data == null) {
                return false;
            }
            //
            return areaBean.getWorldLevel() >= data.getWordlv();
        } catch (Exception e) {
        }
        return false;
    }

    //判断玩家等级开启功能
    public boolean checkPlayer(Function func, String playerCode) {
        try {
            function data = DataCenter.getData(func.getId(), function.class);
            if (data == null) {
                return false;
            }
            PlayerBean playerBean = PlayerBean.get(playerCode);
            return playerBean.getLevel() >= data.getPlayerlv();
        } catch (Exception e) {
        }
        return false;
    }

    //判断征战关数来开启功能
    public boolean checkMapId(Function func, String playerCode) {
        try {
            function data = DataCenter.getData(func.getId(), function.class);
            if (data == null) {
                return false;
            }
            //
            ConquestModule module = ConquestModule.get(playerCode);
            return module.getMapId() >= data.getMapnum();
        } catch (Exception e) {
        }
        return false;
    }

    //已开服时间
    public Long getServerStartTime(PlayerBean playerBean) {
        long days = 0l;
        try {
            ServerBean serverBean = ServerBean.get(playerBean.getServerId());
            Date starttime = serverBean.getStarttime().toDate();
            Calendar instance = Calendar.getInstance();
            instance.setTime(starttime);
            int year = instance.get(Calendar.YEAR);
            int month = instance.get(Calendar.MONTH) + 1; //当前月
            int day = instance.get(Calendar.DAY_OF_MONTH);
            LocalDate startDate = LocalDate.of(year, month, day);
            long betweenDay = ChronoUnit.DAYS.between(startDate, LocalDate.now());
            return betweenDay + 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return days;
    }

    //已开服时间
    public static Long getAlreadlyPassTime(String playerCode) throws ParseException {
        //用户信息
        PlayerBean playerBean = PlayerBean.get(playerCode);
        //服id
        int serverId = playerBean.getServerId();
        //区服信息
        server server = MySQLServerDAO.getFromDAOAccountManager(serverId);
        //开服日期
        Date openTime = server.getOpenTime();
        //当前日期
        Date nowTime = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format1 = simpleDateFormat.format(nowTime);
        String format2 = simpleDateFormat.format(openTime);
        Date now = simpleDateFormat.parse(format1);
        Date before = simpleDateFormat.parse(format2);
        long sub = Math.abs(now.getTime() - before.getTime());
        sub = sub / 1000 / 60 / 60 / 24;
        return sub + 1;
    }


    /**
     * 获取已过去几个月
     *
     * @param playerCode
     * @return
     */
    public static Integer getAlreadlyPassMonth(String playerCode) {
        PlayerBean playerBean = PlayerBean.get(playerCode);
        ServerBean serverBean = ServerBean.get(playerBean.getServerId());
        Date starttime = serverBean.getStarttime().toDate();
        Date now = new Date();
        int monthDiff = ActiveUtil.getMonthDiff(starttime, now);
        return monthDiff;
    }


}
